﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using iTeachSoft.Code.Utility;
using AviFile;
using iTeachSoft.Code.Managers;
using System.Drawing;
using iTeachSoft.CoreObjects;
using iTeachSoft.Presentation.Code.Managers;

namespace iTeachSoft.Presentation.Dialogs
{
    /// <summary>
    /// Interaction logic for DialogExportAsAvi.xaml
    /// </summary>
    public partial class DialogExportAsAvi : Window
    {
        public DialogExportAsAvi()
        {
            InitializeComponent();

            this.buttonOk.Click += new RoutedEventHandler(buttonOk_Click);
            this.buttonCancel.Click += new RoutedEventHandler(buttonCancel_Click);
            this.buttonBrowse.Click += new RoutedEventHandler(buttonBrowse_Click);
        }

        void buttonBrowse_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = ".avi files | *.avi";

            DialogResult result = dlg.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                textBoxFilePath.Text = dlg.FileName;
            }
        }

        void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void buttonOk_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxFilePath.Text)) AlertUtility.ShowError("Please select an output file!");
            else
            {
                if (RuntimeStorage.CurrentProject.Slides.Count > 0)
                {
                    AviManager aviManager = new AviManager(textBoxFilePath.Text, false);
                    if (RuntimeStorage.CurrentProject.Slides.Count <= 0) AlertUtility.ShowError("Currently there are no slides added to the project!");
                    else
                    {
                        VideoStream stream = aviManager.AddVideoStream(checkBoxCompress.IsChecked.Value, 2f, new Bitmap(FileManager.GetSlidePreviewPath(RuntimeStorage.CurrentProject.Slides[0])));

                        Slide slide0 = RuntimeStorage.CurrentProject.Slides[0];
                        Bitmap newFrame0 = (Bitmap)Bitmap.FromFile(FileManager.GetSlidePreviewPath(slide0));
                        //hack to be able to add variable time length
                        for (int index = 0; index < (int)((float)slide0.ShowSlideForSeconds / 0.5f) - 1; index++)
                        {
                            try
                            {
                                stream.AddFrame(new Bitmap(newFrame0));
                            }
                            catch (Exception ex)
                            {
                                AlertUtility.ShowError(ex.Message);
                            }
                        }

                        for (int slidesIndex = 1; slidesIndex < RuntimeStorage.CurrentProject.Slides.Count; slidesIndex++)
                        {
                            Slide slide = RuntimeStorage.CurrentProject.Slides[slidesIndex];
                            Bitmap newFrame = (Bitmap)Bitmap.FromFile(FileManager.GetSlidePreviewPath(slide));
                            //hack to be able to add variable time length
                            for (int index = 0; index < (int)((float)slide.ShowSlideForSeconds / 0.5f); index++)
                            {
                                try
                                {
                                    stream.AddFrame(new Bitmap(newFrame));

                                    if (slidesIndex + 1 == RuntimeStorage.CurrentProject.Slides.Count)
                                        stream.AddFrame(new Bitmap(newFrame));
                                }
                                catch (Exception ex)
                                {
                                    AlertUtility.ShowError(ex.Message);
                                }
                            }
                        }

                        aviManager.Close();
                    }
                }

                this.Close();
            }
        }
    }
}
